1400D - Zigzags - CodeForces Solution


brute force combinatorics data structures math two pointers *1900

Please click on ads to support us..

Python Code:

import os,sys
from random import randint, shuffle
from io import BytesIO, IOBase

from collections import defaultdict,deque,Counter
from bisect import bisect_left,bisect_right
from heapq import heappush,heappop, heapify
from functools import lru_cache, reduce
from itertools import accumulate, permutations
import math, copy

sys.setrecursionlimit(2*10**5+5)

BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

MOD = 998244353  



def main():
    def solve():
        n = int(input())
        arr = list(map(int, input().split()))
        cc = Counter()
        ans = 0
        for i in range(n):
            delta = 0
            for j in range(i+1,n):
                if arr[i] == arr[j]:
                    ans += delta 
                delta += cc[arr[j]]
            cc[arr[i]] += 1
        print(ans)
        return  

        


            t = int(input())
        for i in range(t):
                solve() 
                                                

    

if __name__ == "__main__":
    main()


C++ Code:

#include<bits/stdc++.h>

#define ll long long
#define pp push_back
#define endl '\n'
#define all(x) x.begin(),x.end()
#define ld long double
#define PI acos(-1)
#define sin(a) sin((a)*PI/180)
#define cos(a) cos((a)*PI/180)
#define ones(x) __builtin_popcountll(x)
//#define int ll

#define Drakon  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;


unsigned long long inf = 1e10;
const double EPS = 1e-6;
const int MOD = 1000000007, N = 200005, LOG = 25, M = 2;

typedef array<array<int, M>, M> matrix;

ll gcd(ll x, ll y) {
    return y ? gcd(y, x % y) : x;
}

ll lcm(ll a, ll b) {
    return (a * b) / __gcd(a, b);
}

void solve() {
    int n;
    cin >> n;
    int arr[n];
    vector<int>ind[n + 1];
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
        ind[arr[i]].pp(i);
    }
    ll tmp[n][n];
    for (int i = 0; i < n; ++i) {
        for (int j = i + 2; j < n; ++j) {
            tmp[i][j] = (int)ind[arr[i]].size() - (lower_bound(all(ind[arr[i]]),j) - ind[arr[i]].begin());
        }
    }
    for (int j = 2; j < n; ++j) {
        for (int i = 1; i <= j - 2; ++i) {
            tmp[i][j] += tmp[i - 1][j];
        }
    }

    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        for (int k = i + 2; k < n - 1; ++k) {
            if(arr[i] != arr[k])continue;
            ll add = tmp[k - 1][k + 1] - tmp[i][k + 1];
            ans += add;
        }
    }
    cout << ans << endl;
    
}

signed main() {
    Drakon
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle